package cn.liuding.pethome.org.controller;

import cn.liuding.pethome.basic.util.DepartmentResult;
import cn.liuding.pethome.basic.util.PageList;
import cn.liuding.pethome.org.domain.Department;
import cn.liuding.pethome.org.query.DepartmengQuery;
import cn.liuding.pethome.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.awt.print.Pageable;
import java.util.List;

@RestController//controller+responsebody
//接口文档的注解
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Resource
    private IDepartmentService departmentService;

    @GetMapping("/dept/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department queryById(
            @ApiParam(name="编号",required = true)
            @PathVariable ("id") Long id){
        Department department = departmentService.loadById(id);
        return department;
    }

    @DeleteMapping("/dept/{id}")
    public DepartmentResult delete(@PathVariable ("id") Long id ){
        try{
            departmentService.delete(id);
            return DepartmentResult.create().setSuccess(true).setMessage("删除成功");
        }catch (Exception e){
            return DepartmentResult.create().setSuccess(false).setMessage("服务器异常");
        }

    }

    @PutMapping("/dept")
    public DepartmentResult save(@RequestBody Department department){
        try{
            if(department.getId()==null){
                departmentService.add(department);
            }else{
                departmentService.update(department);
            }
            return DepartmentResult.create().setSuccess(true).setMessage("保存成功");
        }catch (Exception e){
            return DepartmentResult.create().setSuccess(false).setMessage("服务器异常");
        }

    }

    @GetMapping("/dept")
    public List<Department> queryAll(){
        List<Department> list = departmentService.loadAll();
        return list;
    }

    @PostMapping("/dept")
    public PageList<Department> page(@RequestBody DepartmengQuery query){
        return departmentService.pageList(query);
    }

    @PatchMapping("/dept")
    public DepartmentResult deleteAll(@RequestBody List<Long> ids){
        try{
            departmentService.deleteAll(ids);
            return DepartmentResult.create().setSuccess(true).setMessage("删除成功");
        }catch (Exception e){
            return DepartmentResult.create().setSuccess(false).setMessage("服务器异常");
        }
    }

    @GetMapping("/dept/tree")
    public List<Department> treeData(){
        List<Department> list = departmentService.treeData();
        return list;
    }

}
